package com.example.utils;

import com.example.entity.Content;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject: elasticsearch-jd
 * @BelongsPackage: com.example.utils
 * @Author: yanhongwei
 * @CreateTime: 2022-04-02 11:17
 * @Description: //TODO
 */
@Component
public class HtmlParseUtil {

    public static void main(String[] args) throws IOException {
        new HtmlParseUtil().parseJd("数学").forEach(System.out::println);

    }
    public List<Content> parseJd(String keyword)throws IOException {
        String url = "https://search.jd.com/Search?keyword="+keyword;

        //解析url
        Document document = Jsoup.parse(new URL(url), 30000);
        Element list = document.getElementById("J_goodsList");
        //获取li元素
        Elements li = list.getElementsByTag("li");

        List<Content> goodsList = new ArrayList<>();

        for (Element elements : li) {

            //图片懒加载
            String title = elements.getElementsByClass("p-name").eq(0).text();
            String img = elements.getElementsByTag("img").eq(0).attr("data-lazy-img");
            String price = elements.getElementsByClass("p-price").eq(0).text();

            Content content = new Content();
            content.setTitle(title);
            content.setImg(img);
            content.setPrice(price);
            goodsList.add(content);
        }
        return goodsList;
    }
}
